home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Tools & Apps / OS⁄Toolbox / MDEF (LS Pascal) / Pat Menu Demo main < prev    next >
Encoding:
Text File  |  1990-04-10  |  10.4 KB  |  483 lines  |  [TEXT/PJMM]

  1. program pat_menu_demo;
  2.  
  3.     uses
  4.         UDPInstall, utilities, mdef;
  5.  
  6.     const
  7.         WindowID = 300;
  8.         AboutID = 1000;
  9.         MYPATLIST = 1000;
  10.         QuitItem = 1;
  11.         active = 0;
  12.         inactive = 255;
  13.         AppleID = 150;
  14.         FileID = 151;
  15.         EditID = 152;
  16.         GraphicID = 153;
  17.         UndoItem = 1;
  18.         CutItem = 3;
  19.         CopyItem = 4;
  20.         PasteItem = 5;
  21.         ClearItem = 6;
  22.  
  23.     var
  24.         aWindow: WindowPtr;
  25.         AppleMenu: MenuHandle;
  26.         FileMenu: MenuHandle;
  27.         EditMenu: MenuHandle;
  28.         GraphicMenu: MenuHandle;
  29.         testPattern: Pattern;
  30.         theEvent: EventRecord;
  31.         Finished: Boolean;
  32.         quitonwindclose: Boolean;
  33.         changeFlag: longint;
  34.         aboutW: WindowPtr;
  35.         aRect, cRect: Rect;
  36.         aStr: Str255;
  37.         aboutEvt: EventRecord;
  38.         aboutDone: Boolean;
  39.  
  40.  
  41.     procedure drawabout;
  42.  
  43.     begin        { Draw the contents of the "About..." window }
  44.         TextFont(0);
  45.         TextSize(12);
  46.         aStr := 'A Pattern Menu Demo ';
  47.         TextFace([condense]);
  48.         MoveTo(((aboutW^.portRect.right) - StringWidth(aStr)) div 2, 32);
  49.         DrawString(aStr);
  50.         TextFace([]);
  51.         TextFont(3);
  52.         TextSize(9);
  53.         aStr := 'by Galen Babcock    Translated from Lightspeed C by David Schwan';
  54.         MoveTo(((aboutW^.portRect.right) - StringWidth(aStr)) div 2, 44);
  55.         DrawString(aStr);
  56.         aStr := 'Written & Compiled with Lightspeed Pascal from THINK Technologies , Inc . ';
  57.         MoveTo(((aboutW^.portRect.right) - StringWidth(aStr)) div 2, 56);
  58.         DrawString(aStr);
  59.         aStr := 'therefore , portions Copyright © 1988 by THINK Technologies , Inc . ';
  60.         MoveTo(((aboutW^.portRect.right) - StringWidth(aStr)) div 2, 68);
  61.         DrawString(aStr);
  62.         aStr := 'Copyright © 1987 by Galen Babcock ';
  63.         MoveTo(aboutW^.portRect.left + 4, aboutW^.portRect.bottom - 4);
  64.         DrawString(aStr);
  65.         aStr := 'Version 1.0 ';
  66.         MoveTo(aboutW^.portRect.right - StringWidth(aStr) - 4, aboutW^.portRect.bottom - 4);
  67.         DrawString(aStr);
  68.     end;
  69.  
  70.     procedure doabout;
  71.  
  72.         var
  73.             tempPort: GrafPtr;
  74.  
  75.     begin        { rather than using a dialog, just create a window to draw the "About..." stuff }
  76.         GetPort(tempPort);
  77.         InitCursor;
  78.         SetRect(aRect, 0, 0, 340, 120);
  79.         centerrect(aRect, screenBits.bounds);
  80.         aboutW := NewWindow(nil, aRect, ' Window ', FALSE, 3, WindowPtr(-1), FALSE, 0);
  81.         SetPort(aboutW);
  82.         zoomport(aboutW, TRUE);
  83.         aboutDone := FALSE;        { do our own event - handling until the}
  84. { user either clicks the mouse , or presses a key on the keyboard}
  85.         repeat
  86.             begin
  87.                 if (GetNextEvent(everyEvent, aboutEvt)) then
  88.                     begin
  89.                         case aboutEvt.what of
  90.                             updateEvt: 
  91.                                 begin
  92.                                     BeginUpdate(aboutW);
  93.                                     drawabout;
  94.                                     EndUpdate(aboutW);
  95.                                 end;
  96.  
  97.                             keyDown, autoKey, mouseDown: 
  98.                                 aboutDone := TRUE;
  99.  
  100.                             otherwise
  101.                                 ;
  102.                         end;
  103.                     end;
  104.             end;
  105.         until aboutDone;
  106.         HideWindow(aboutW);
  107.         zoomport(aboutW, FALSE);
  108.         DisposeWindow(aboutW);
  109.         SetPort(tempPort);
  110.     end;
  111.  
  112.     procedure GracefulExit;
  113.  
  114.     begin
  115.         ExitToShell;    { just return to Finder on system errors }
  116.     end;
  117.  
  118.     procedure Init;
  119.  
  120.         var
  121.             index: integer;
  122.  
  123.     begin
  124.         MaxApplZone;
  125.         for index := 1 to 11 do
  126.             MoreMasters;
  127.         InitGraf(@thePort);
  128.         InitFonts;
  129.         FlushEvents(everyEvent, 0);
  130.         InitWindows;
  131.         InitMenus;
  132.         TEInit;
  133.         InitDialogs(@GracefulExit);
  134.         InitCursor;
  135.         GetIndPattern(testPattern, MYPATLIST, 1);
  136.         quitonwindclose := TRUE;    { closing application window exits application }
  137.         randSeed := TickCount;
  138.     end;
  139.  
  140.     procedure UpdateEdit (OnOff: boolean);
  141.  
  142.         var
  143.             index: integer;
  144.  
  145. { if OnOff is true, then enable Edit menu items, otherwise }
  146. { disable all of the Edit menu items. }
  147.  
  148.     begin
  149.         for index := 1 to 7 do
  150.             if OnOff then
  151.                 EnableItem(EditMenu, index)
  152.             else
  153.                 DisableItem(EditMenu, index);
  154.         DisableItem(EditMenu, 2);
  155.     end;
  156.  
  157.     procedure makemenus;
  158.  
  159.     begin
  160.         AppleMenu := GetMenu(AppleID);
  161.         AddResMenu(AppleMenu, 'DRVR');
  162.         InsertMenu(AppleMenu, 0);
  163.         FileMenu := GetMenu(FileID);
  164.         InsertMenu(FileMenu, 0);
  165.         EditMenu := GetMenu(EditID);
  166.         InsertMenu(EditMenu, 0);
  167.         UpdateEdit(FALSE);
  168.         GraphicMenu := NewMenu(GraphicID, 'Pattern');
  169. { set the MenuHandle.menuProc to the MDEF #130 in our }
  170. { resource file }
  171.         InstallDefProc(CurResFile, 'MDEF', 130, @Our_Custom_Menu);
  172.         GraphicMenu^^.menuProc := GetResource('MDEF', 130);
  173.         CalcMenuSize(GraphicMenu);        { calculate how large the menu rectangle is }
  174.         InsertMenu(GraphicMenu, 0);
  175.         DrawMenuBar;
  176.     end;
  177.  
  178.     procedure makewindow;
  179.  
  180.         var
  181.             wRect: Rect;
  182.             FontStuff: FontInfo;
  183.  
  184.     begin
  185.         SetRect(wRect, 0, 0, 220, 150);        { make an application window }
  186.         aWindow := NewWindow(nil, wRect, 'Pattern Demo', FALSE, 8, WindowPtr(-1), TRUE, 0);
  187.         SetPort(aWindow);
  188.         TextFont(0);
  189.         TextSize(12);
  190.         TextFace([bold]);
  191.         TextMode(srcCopy);
  192.         centerwindow(aWindow, screenBits.bounds);
  193.         zoomport(aWindow, TRUE);
  194.     end;
  195.  
  196.     procedure DoMenu (mChoice: longint);
  197.  
  198.         var
  199.             MenuID, MenuItem, i: integer;
  200.             accName: Str255;
  201.             tempPort: GrafPtr;
  202.  
  203.     begin
  204.         MenuID := HiWord(mChoice);
  205.         MenuItem := LoWord(mChoice);
  206.         case MenuID of
  207.             AppleID: 
  208.                 case MenuItem of
  209.                     1: 
  210.                         doabout;
  211.  
  212.                     otherwise
  213.                         begin
  214.                             GetItem(AppleMenu, MenuItem, accName);
  215.                             i := OpenDeskAcc(accName);
  216.                         end;
  217.                 end;
  218.  
  219.             FileID: 
  220.                 case MenuItem of
  221.                     QuitItem: 
  222.                         Finished := TRUE;
  223.                     otherwise
  224.                         ;
  225.                 end;
  226.  
  227.             EditID: 
  228.                 if not SystemEdit(MenuItem - 1) then
  229.                     case MenuItem of
  230.                         UndoItem: 
  231.                             ;
  232.                         CutItem: 
  233.                             ;
  234.                         CopyItem: 
  235.                             ;
  236.                         PasteItem: 
  237.                             ;
  238.                         ClearItem: 
  239.                             ;
  240.                         otherwise
  241.                             ;
  242.                     end;
  243.  
  244.             GraphicID: 
  245.                 if MenuItem > 0 then
  246.                     begin
  247.                         GetIndPattern(testPattern, MYPATLIST, MenuItem);
  248.                         GetPort(tempPort);
  249.                         SetPort(aWindow);
  250.                         InvalRect(aWindow^.portRect);
  251.                         SetPort(tempPort);
  252.                     end;
  253.  
  254.             otherwise
  255.                 ;
  256.         end;
  257.         HiliteMenu(0);
  258.     end;
  259.  
  260.     procedure DoDrag (aPoint: Point;
  261.                                     eventWindow: WindowPtr);
  262.  
  263. { drag the application window around within screenBits.bounds }
  264.  
  265. { If eventWindow != FrontWindow(), don't bring it to front, this}
  266. { way a command-drag can move a background window without }
  267. { bringing it to the front, according to the Macintosh user }
  268. { interface guidelines }
  269.  
  270.     begin
  271.         DragWindow(eventWindow, aPoint, screenBits.bounds);
  272.     end;
  273.  
  274.     procedure DoContent (aPoint: Point;
  275.                                     eventWindow: WindowPtr);
  276.  
  277.         var
  278.             thePart, index, pole: integer;
  279.             theControl: ControlHandle;
  280.             cRect: Rect;
  281.  
  282.     begin
  283.         if FrontWindow <> eventWindow then
  284.             SelectWindow(eventWindow)
  285.         else
  286.             GlobalToLocal(aPoint);    { handle any application-specific mousedown events here }
  287.     end;
  288.  
  289.     procedure dogrow (aPoint: Point;
  290.                                     eventWindow: WindowPtr);
  291.  
  292.         var
  293.             newSize: longint;
  294.             newWidth, newHeight: integer;
  295.             limitRect: Rect;
  296.  
  297.     begin
  298.         if FrontWindow <> eventWindow then
  299.             SelectWindow(eventWindow)
  300.         else
  301.             begin
  302. { set a limit rectangle for minimum and maximum window size }
  303.                 SetRect(limitRect, 100, 100, 32700, 32700);
  304.                 newSize := GrowWindow(eventWindow, aPoint, limitRect);
  305.                 if newSize <> 0 then
  306.                     begin
  307.                         newWidth := LoWord(newSize);
  308.                         newHeight := HiWord(newSize);
  309.                         SizeWindow(eventWindow, newWidth, newHeight, TRUE);
  310.                         InvalRect(eventWindow^.portRect);
  311.                     end;
  312.             end;
  313.     end;
  314.  
  315.     procedure DoGoAway (aPoint: Point;
  316.                                     eventWindow: WindowPtr);
  317.  
  318. { if the global Boolean "quitonwindclose" is set to true, then}
  319. { closing the application window will also exit the application.}
  320. { otherwise, the window just goes away.}
  321.  
  322.     begin
  323.         if (TrackGoAway(eventWindow, aPoint)) then
  324.             Finished := quitonwindclose;
  325.     end;
  326.  
  327.     procedure handlemousedown;
  328.  
  329.         var
  330.             mouseWhere: Point;
  331.             whichWindow: WindowPtr;
  332.             Part: integer;
  333.  
  334. { we have a mousedown event, find out where the mouse was}
  335. { clicked, and dispatch to proper mousedown handling routines}
  336.  
  337.     begin
  338.         Part := FindWindow(theEvent.where, whichWindow);
  339.         case Part of
  340.             inSysWindow: 
  341.                 SystemClick(theEvent, whichWindow);
  342.  
  343.             inMenuBar: 
  344.                 DoMenu(MenuSelect(theEvent.where));
  345.  
  346.             inDrag: 
  347.                 DoDrag(theEvent.where, whichWindow);
  348.  
  349.             inContent: 
  350.                 DoContent(theEvent.where, whichWindow);
  351.  
  352.             inGrow: 
  353.                 dogrow(theEvent.where, whichWindow);
  354.  
  355.             inGoAway: 
  356.                 DoGoAway(theEvent.where, whichWindow);
  357.  
  358.             inZoomIn, inZoomOut: 
  359.                 if (TrackBox(whichWindow, theEvent.where, Part)) then
  360.                     begin
  361.                         ZoomWindow(whichWindow, Part, TRUE);
  362.                         InvalRect(whichWindow^.portRect);
  363.                     end;
  364.  
  365.             otherwise
  366.                 ;
  367.         end;
  368.     end;
  369.  
  370.     procedure handlekeydown;
  371.  
  372.         var
  373.             ch: char;
  374.             chCode: integer;
  375.             menuChoice, newTime: longint;
  376.             charRect: Rect;
  377.  
  378. { was the command-key down?  If so, it might be a command key}
  379. { equivalent for a menu item}
  380.  
  381.     begin
  382.         ch := char(BitAnd(theEvent.message, charCodeMask));
  383.         if BitAnd(theEvent.modifiers, cmdKey) <> 0 then
  384.             DoMenu(MenuKey(ch))
  385.         else
  386.             ;
  387.     end;
  388.  
  389.     procedure handleactivate;
  390.  
  391.         var
  392.             eventWindow: WindowPtr;
  393.  
  394.     begin
  395.         eventWindow := WindowPtr(theEvent.message);
  396.         SetPort(eventWindow);
  397.         if eventWindow = aWindow then
  398.             begin
  399.                 DrawGrowIcon(eventWindow);
  400.                 if BitAnd(theEvent.modifiers, activeFlag) <> 0 then
  401.                     begin        { our window just got an activate event }
  402.                         if BitAnd(theEvent.modifiers, changeFlag) <> 0 then
  403.                             UpdateEdit(FALSE);
  404.                     end
  405.                 else
  406.                     begin        { our window just got a deactivate event }
  407.                         if BitAnd(theEvent.modifiers, changeFlag) <> 0 then
  408.                             UpdateEdit(TRUE);
  409.                     end;
  410.             end;
  411.     end;
  412.  
  413.     procedure handleupdate;
  414.  
  415.         var
  416.             tempPort, thisPort: GrafPtr;
  417.             dummyRect: Rect;
  418.  
  419.     begin
  420.         GetPort(tempPort);
  421.         thisPort := WindowPtr(theEvent.message);
  422.         SetPort(thisPort);
  423.         if thisPort = aWindow then
  424.             begin
  425.                 BeginUpdate(thisPort);
  426.                 EraseRect(thisPort^.portRect);
  427.                 DrawGrowIcon(thisPort);
  428. { clip drawing to exclude the scroll bar areas }
  429. { since our window has a growicon }
  430.                 dummyRect := thisPort^.portRect;
  431.                 dummyRect.right := dummyRect.right - 15;
  432.                 dummyRect.bottom := dummyRect.bottom - 15;
  433.                 ClipRect(dummyRect);
  434. { our application-specific window drawing routines }
  435.                 FillRect(thisPort^.portRect, testPattern);
  436. { after drawing, set the clip region to an }
  437. { arbitrarily large rectangle }
  438.                 SetRect(dummyRect, -32000, -32000, 32000, 32000);
  439.                 ClipRect(dummyRect);
  440.                 EndUpdate(thisPort);
  441.             end;
  442.         SetPort(tempPort);
  443.     end;
  444.  
  445.     procedure handleanevent;
  446.  
  447. { find out what kind of event occured, and dispatch to the }
  448. { proper event-handling routines }
  449.  
  450.     begin
  451.         case theEvent.what of
  452.             mouseDown: 
  453.                 handlemousedown;
  454.  
  455.             keyDown, autoKey: 
  456.                 handlekeydown;
  457.  
  458.             activateEvt: 
  459.                 handleactivate;
  460.  
  461.             updateEvt: 
  462.                 handleupdate;
  463.  
  464.             otherwise
  465.                 ;
  466.         end;
  467.     end;
  468.  
  469. begin
  470.     Init;
  471.     makemenus;
  472.     makewindow;
  473.     Finished := FALSE;
  474.     { the event loop }
  475.     while (Finished = FALSE) do
  476.         begin
  477.             SystemTask;
  478.             if (GetNextEvent(everyEvent, theEvent)) then
  479.                 handleanevent;
  480.         end;
  481. {    SetCursor(CursHandle(GetCursor(watchCursor)));}
  482.     zoomport(aWindow, FALSE);
  483. end.